package cn.jiedanba.cacert.caadmin.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;

import com.alibaba.fastjson.JSON;

import cn.jiedanba.cacert.common.result.ResponseResult;
import cn.jiedanba.cacert.common.session.UserLoginSession;
import cn.jiedanba.cacert.common.util.ServletUtils;

public class LoginInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		boolean isLogin = false;
		if (UserLoginSession.user() != null) {
			isLogin = true;

		}
		if (ServletUtils.isAjaxRequest(request)) {
			if (!isLogin) {
				ServletUtils.renderString(response, JSON.toJSONString(ResponseResult.fail("未登录或登录已超时")));
				return isLogin;
			}
		} else {
			if (!isLogin) {
				response.sendRedirect("/login");
				return isLogin;
			}
		}
		return true;
	}
}
